﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public partial class UserInfoBLL
    {
        public UserInfo Login(string strName, string strPwd)
        {
            //1.调用业务层方法 根据登陆名查询
            UserInfo usr = GetList(u => u.loginname == strName).FirstOrDefault();
            //2.判断是否登陆成功
            if (usr != null && usr.pwd == Tools.Security.MD5(strPwd))
            {
                return usr;
            }
            return null;
        }

        /// <summary>
        /// 分页查询，加排序. 例如    bll.GetPageList(u => u.username == "姓名", pg,b=> b.id,OrderByType.Desc);
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="page"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public List<UserInfo> GetPageListRoname(PageModel page,string username,int shopid)
        {
            
            int count = 0;
            var list = Db.Queryable<UserInfo, UserRole, RoleInfo>((sa, sb, sc) => new object[] {
                JoinType.Left,sa.id==sb.uid,
                JoinType.Left,sb.rid==sc.id
              })
          .Where((sa, sb, sc) => sa.isDel == false)
          .WhereIF(!string.IsNullOrEmpty(username), (sa, sb, sc) => sa.username.Contains(username))
          .WhereIF(shopid!=0, (sa, sb, sc) => sa.Shopid== shopid)
          .Select((sa, sb, sc) => new UserInfo { id = sa.id,
              loginname = sa.loginname,
              username = sa.username,
              gender = sa.gender,
              Rolename = sc.rName,
              ShopName =sa.ShopName,
              Shopid=sa.Shopid,
              Rid=sb.rid,
              downNum=sa.downNum
          })
          .ToPageList(page.PageIndex, page.PageSize, ref count);
            page.PageCount = count;
            return list;
        }

        /// <summary>
        /// 分页查询，加排序. 例如    bll.GetPageList(u => u.username == "姓名", pg,b=> b.id,OrderByType.Desc);
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="page"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public UserInfo GetUserhasRole(int uid)
        {

            UserInfo user = Db.Queryable<UserInfo, UserRole, RoleInfo>((sa, sb, sc) => new object[] {
                JoinType.Left,sa.id==sb.uid,
                JoinType.Left,sb.rid==sc.id
              })
          .Where((sa, sb, sc) => sa.isDel == false && sa.id == uid)
          .OrderBy((sa) => sa.id)
          .Select((sa, sb, sc) => new UserInfo { id = sa.id, loginname = sa.loginname, username = sa.username, gender = sa.gender, Rolename = sc.rName ,Shopid=sa.Shopid }).ToList().FirstOrDefault();
        
            return user;
        }

    }
}
